package com.entor.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;

import com.entor.model.Role;
import com.entor.model.Student;

@Mapper
public interface StudentDao {
	
	@Select("<script>select * from student where 2=2 "
			+"<when test='id!=null'> and  id= ${id}</when>"
			+"<when test='loginName!=null'> and  login_name= #{loginName}</when>"
	+"</script>")
	@Results({
		@Result(column="login_name",property="loginName")
		
	})
	public List<Student> find(Student s);
	
	@Select("select * from student where login_name = #{name}")
	public Student getByName(@Param(value="name") String name);
	
	
	@Update({"<script>update student set ",
			"<when test='loginName!=null'>login_name = #{loginName},</when>",
			"<when test='userName!=null'>user_name = #{userName},</when>",
			" id=${id} where id= ${id}</script>"})
	public int update(Student s);
	
	@Delete({"delete from student where id = ${id}"})
	public int delete(@Param(value="id") Integer id);
	
	@Insert({"insert into student(login_name,user_name,password,sex) ",
		" values ",
		" (#{loginName},#{userName},#{password},#{sex})"})
	/**
	 *  返回自动增长主键(mysql)
	 */
	@Options(useGeneratedKeys=true,keyProperty="id")
	public int insert(Student s);
	
	
	/**
	 * oracle 主键非自动增长
	 */
	@Insert({"insert into t_urc_role (id,name) ",
		" values ",
		"(seq_urc_id.nextval,#{name})"})
	@SelectKey(statement="select seq_urc_id.currval from dual",keyProperty="id",before=false,resultType=Integer.class )
	public int insertRole(Role r);
	
	
	
	
	

}
